package com.yhj.reggie.controller;

import com.yhj.reggie.common.R;
import com.yhj.reggie.dto.DishDto;
import com.yhj.reggie.entity.Dish;
import com.yhj.reggie.entity.Page;
import com.yhj.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto, HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(id);
        dishDto.setUpdateUser(id);

        dishService.save(dishDto);

        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page<DishDto>> findByPage2(Integer page, Integer pageSize, String name){
        Page<DishDto> resultPage = dishService.findByPage2(page, pageSize, name);
        return R.success(resultPage);
    }
    @GetMapping("/{id}")
    public R<DishDto> findById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto,HttpSession session){
        Long id = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(id);
        dishService.update(dishDto);
        return R.success("修改成功");
    }

    @RequestMapping("/list")
    public R<List<DishDto>> findByCid(Long categoryId,Integer status){
        List<DishDto> dishList = dishService.findByCid(categoryId, status);
        return R.success(dishList);
    }


}
